package com.seeyon.apps.ddkk.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.seeyon.apps.kk.constants.Constants;
import com.seeyon.apps.kk.manager.AdapterSyncDataManager;
import com.seeyon.apps.kk.util.JdbcAgentUtil;
import com.seeyon.apps.kk.util.TTUtils;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.exceptions.BusinessException;

public class SyncDDDistributorManager extends AdapterSyncDataManager {
	private static final Log log = LogFactory.getLog(SyncDDDistributorManager.class); 
	private String url;
	private String token;
	private String interfaceNo;
	@Override
	public boolean isUnflow() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public String getFormCode() {
		// TODO Auto-generated method stub
		return Constants.ddBaseCode;
	}

	@Override
	public String getSendUser() {
		// TODO Auto-generated method stub
		return Constants.createUser;
	}

	@Override
	public void delHistoryData() {
		// TODO Auto-generated method stub
		try {
			FormBean formBean = formApi4Cap4.getFormByFormCode(getFormCode());
			FormTableBean formTableBean = formBean.getMasterTableBean();
			String sql="delete from "+formTableBean.getTableName();
			List  list=new ArrayList();
			JdbcAgentUtil.doUpdateOrInsert(sql,list);
		} catch (BusinessException e) {
			log.info("经销商上数据同步删除数据异常:"+e);
		}
	}

	@Override
	public List<Map> getSyncData() {
		// TODO Auto-generated method stub
	    JSONArray dataAry=TTUtils.simsResource(url, token, interfaceNo);
		return dataAry.toJavaList(Map.class);
		/*return null;*/
	}

	@Override
	public Map<String, Object> data2FormMap(Map params) {
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("经销商组代码", MapUtils.getObject(params, "UdstrCd"));
		map.put("经销商名称", MapUtils.getObject(params, "UdstrDescCn"));
		map.put("UdstrDescEn", MapUtils.getObject(params, "UdstrDescEn"));
		map.put("Email", MapUtils.getObject(params, "Email"));
		map.put("Address", MapUtils.getObject(params, "Address"));
		map.put("ZipCd", MapUtils.getObject(params, "ZipCd"));
		map.put("PhoneNo", MapUtils.getObject(params, "PhoneNo"));
		map.put("Udstr_Remark1", MapUtils.getObject(params, "Remark1"));
		map.put("Udstr_Remark2", MapUtils.getObject(params, "Remark2"));
		map.put("FaxNo",MapUtils.getObject(params, "FaxNo"));
		map.put("DD_DATE_D2", MapUtils.getObject(params, "DDDateD2"));
		map.put("Udstr_Remark3", MapUtils.getObject(params, "Remark3"));
		map.put("Udstr_Remark4", MapUtils.getObject(params, "Remark4"));
		map.put("Director", MapUtils.getObject(params, "Director"));
		map.put("DirectorNo", MapUtils.getObject(params, "DirectorNo"));
		map.put("DirectorMobile",MapUtils.getObject(params, "DirectorMobile"));
		map.put("DirectorEmail",MapUtils.getObject(params, "DirectorEmail"));
		map.put("FinanceContact",MapUtils.getObject(params, "FinanceContact"));
		map.put("FinanceContactNo",MapUtils.getObject(params, "FinanceContactNo"));
		map.put("FinanceContactMobile",MapUtils.getObject(params, "FinanceContactMobile"));
		map.put("FinanceEmail",MapUtils.getObject(params, "FinanceEmail"));
		map.put("CompanyCd", MapUtils.getObject(params, "CompanyCd"));
		map.put("PaymentBy", MapUtils.getObject(params, "PaymentByName"));
		map.put("SimsCd", MapUtils.getObject(params, "SimsCd"));
		map.put("UdstrTypeName", MapUtils.getObject(params, "UdstrTypeName"));
		map.put("CreditLvlName", MapUtils.getObject(params, "CreditLvlName"));
		map.put("ProvinceName", MapUtils.getObject(params, "ProvinceName"));
		map.put("CityName", MapUtils.getObject(params, "CityName"));
		map.put("CoopTypeName", MapUtils.getObject(params, "UdstrCoopTypeName"));
		map.put("子分销商代码", MapUtils.getObject(params, "DstrCD"));
		map.put("Dstr_Desc_CN",MapUtils.getObject(params, "DstrDescCN"));
		map.put("Dstr_Desc_EN", MapUtils.getObject(params, "DstrDescEN"));
		map.put("Email1",MapUtils.getObject(params, "Email1"));
		map.put("Email2", MapUtils.getObject(params, "Email2"));
		map.put("Zip_CD", MapUtils.getObject(params, "ZipCd"));
		map.put("Dstr_PhoneNo",MapUtils.getObject(params, "ZipCd"));
		map.put("Dstr_FaxNo", "");//----------------------------------
		map.put("Dstr_Remark1", MapUtils.getObject(params, "Dstr_Remark1"));
		map.put("Dstr_Remark2", MapUtils.getObject(params, "Dstr_Remark2"));
		map.put("Dstr_Remark3", MapUtils.getObject(params, "Dstr_Remark3"));
		map.put("Dstr_Remark4", MapUtils.getObject(params, "Dstr_Remark4"));
		map.put("CoopStatus", MapUtils.getObject(params, "CoopStatus"));
		map.put("Dstr_Director", "");//----------------------------------
		map.put("Dstr_Director_NO", "");//----------------------------------
		map.put("Dstr_Director_Mobile", "");//----------------------------------
		map.put("Dstr_Finance_Contact", "");//----------------------------------
		map.put("Dstr_Finance_Contact_NO", "");//----------------------------------
		map.put("Dstr_Finance_Contact_Mobile", "");//----------------------------------
		map.put("Fd_Operator", "");//----------------------------------
		map.put("Fd_OperatorNO", "");//----------------------------------
		map.put("Upload_DT", MapUtils.getObject(params, "UploadDT"));
		map.put("Franchise", MapUtils.getObject(params, "Franchise"));
		//kk.do Company_CD字段是否为09200并且BU为EU，BU改为EU1
		String bu=MapUtils.getString(params, "CompanyCD").equals("09200")&&MapUtils.getString(params, "BU").equals("EU")?"EU1":MapUtils.getString(params, "BU");
		map.put("BU", bu);
		map.put("Dstr_Coop_Start_DT", MapUtils.getObject(params, "DstrCoopStartDT"));
		map.put("Dstr_Coop_End_DT", MapUtils.getObject(params, "DstrCoopEndDT"));
		map.put("Dstr_Coop_Status_CD", MapUtils.getObject(params, "DstrCoopStatusCD"));
		map.put("Parent_Dstr_CD", MapUtils.getObject(params, "ParentDstrCD"));
		map.put("Parent_Dstr_Desc_CN", MapUtils.getObject(params, "ParentDstrDescCN"));
		map.put("Biz_Contact", "");//----------------------------------
		map.put("Biz_Contact_NO", "");//----------------------------------
		map.put("Biz_Contact_Mobile", "");//----------------------------------
		map.put("Company_CD", MapUtils.getObject(params, "CompanyCD"));
		map.put("CURRENCY_ID", MapUtils.getObject(params, "CurrencyID"));
		map.put("SAP_Code", MapUtils.getObject(params, "SAPCD"));
		map.put("Payment_By", MapUtils.getObject(params, "PaymentByCD"));
		map.put("DstrTypeName", MapUtils.getObject(params, "DstrTypeName"));
		map.put("DstrCoopTypeName", MapUtils.getObject(params, "DstrCoopTypeName"));
		map.put("FdSoftwareName", MapUtils.getObject(params, "FdSoftwareName"));
		map.put("FdStatuseName", MapUtils.getObject(params, "FdStatusName"));
		map.put("DstrCoopStatusName", MapUtils.getObject(params, "DstrCoopStatusName"));
		map.put("DstrDstrLvlName", MapUtils.getObject(params, "DstrLvlName"));
		map.put("DstrDstrClassName", MapUtils.getObject(params, "DstrClassName"));
		map.put("DstrCreditLvlName", "");//----------------------------------
		map.put("DstrProvinceName", "");//----------------------------------
		map.put("DstrCityName", "");//----------------------------------
		map.put("CurrencyName", MapUtils.getObject(params, "CurrencyName"));
		map.put("iChannel_Email",MapUtils.getObject(params, "iChannelEmail"));
		map.put("iChannel_Mobile", MapUtils.getObject(params, "iChannelMobile"));
		map.put("iChannel_Contact",MapUtils.getObject(params, "iChannelContact"));
		map.put("iChannel_Contact_No",MapUtils.getObject(params, "iChannelContactNo"));
		map.put("BusinessContact", MapUtils.getObject(params, "BusinessContact"));
		map.put("BusinessNo", MapUtils.getObject(params, "BusinessNo"));
		map.put("BusinessMobile", MapUtils.getObject(params, "BusinessMobile"));
		map.put("BusinessEmail",MapUtils.getObject(params, "BusinessEmail"));
		map.put("DataContact",MapUtils.getObject(params, "DataContact"));
		map.put("DataNo", MapUtils.getObject(params, "DataNo"));
		map.put("DataMobile", MapUtils.getObject(params, "DataMobile"));
		map.put("DataEmail", MapUtils.getObject(params, "DataEmail"));
		map.put("Contractor", MapUtils.getObject(params, "Contractor"));
		map.put("ContractorNo",MapUtils.getObject(params, "ContractorNo"));
		map.put("ContractorMobile", MapUtils.getObject(params, "ContractorMobile"));
		map.put("ContractorEmail", MapUtils.getObject(params, "ContractorEmail"));
		map.put("DD_Date", MapUtils.getObject(params, "DDDate"));
		map.put("DD_DATE_END", MapUtils.getObject(params, "DDDateEnd"));
		map.put("DD_Status", MapUtils.getObject(params, "DDStatus"));
		map.put("DD_DATE_END_D2", MapUtils.getObject(params, "DDDateEndD2"));
		map.put("DD_STATUS_D2", MapUtils.getObject(params, "DDStatusD2"));
		map.put("DD_DATE_SERVICE", MapUtils.getObject(params, "DDDateService"));
		map.put("DD_DATE_END_SERVICE", MapUtils.getObject(params, "DDDateEndService"));
		map.put("DD_STATUS_SERVICE", MapUtils.getObject(params, "DDStatusService"));
		map.put("UPDATE_DT", MapUtils.getObject(params, "UpdateDT"));
		map.put("DSM_Email", MapUtils.getObject(params, "DSMEmailAll"));
		map.put("RSM_Email", MapUtils.getObject(params, "RSMEmailAll"));
		map.put("BU_DSM_Email", MapUtils.getObject(params, "DSMEmailBU"));
		map.put("BU_RSM_Email", MapUtils.getObject(params, "RSMEmailBU"));
		return map;
	}

	@Override
	public Object vaildateDataUnique() {
		// TODO Auto-generated method stub
/*		Map<String, Object> map =new HashMap<String, Object>();
		map.put("main","经销商组代码,BU,DstrDstrLvlName");*/
		return null;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getInterfaceNo() {
		return interfaceNo;
	}

	public void setInterfaceNo(String interfaceNo) {
		this.interfaceNo = interfaceNo;
	}
}
